﻿@page "/Agent"
@page "/AgentGroup"
@page "/AgentLog/{Id:guid?}"
@page "/AgentLog/{Action}/{Id:guid?}"
@using Gurux.DLMS.AMI.Shared.DIs
@using Microsoft.AspNetCore.Authorization
@using Microsoft.AspNetCore.Components.WebAssembly.Authentication
@using Gurux.DLMS.AMI.Shared.DTOs.Authentication
@using Gurux.DLMS.AMI.Shared
@using Gurux.DLMS.AMI.Shared.Enums
@using Gurux.DLMS.AMI.Shared.DTOs
@using System.Diagnostics

@attribute [Authorize(Roles = GXRoles.Admin + "," + GXRoles.Agent + "," +
    GXRoles.AgentManager + "," + GXRoles.AgentGroup + "," + GXRoles.AgentGroupManager +
    GXRoles.AgentLog + "," + GXRoles.AgentLogManager)]
@inject NavigationManager NavigationManager
@inject HttpClient Http
@inject IGXNotifier2 Notifier
@implements IDisposable

<!--If user adds a new item or modifies the existing one.-->
@if (Path != null && string.Compare(Action, "Add", true) == 0 || (Id != null && string.Compare(Action, "Edit", true) == 0))
{
    @if (string.Compare(Path, "AgentGroup", true) == 0)
    {
        <AgentGroupTab Action="@Action" Id="@Id"></AgentGroupTab>
    }
    else if (string.Compare(Path, "Agent", true) == 0)
    {
        @if (string.Compare(Action, "Add", true) == 0)
        {
            <AgentAdd></AgentAdd>
        }
        else
        {
            <AgentTab Action="@Action" Id="@Id"></AgentTab>
        }
    }
    else if (string.Compare(Path, "AgentLog", true) == 0)
    {
        <AgentLog Action="@Action" Id="@Id"></AgentLog>
    }
}
else
{
    <TabControl Active="@Path" SelectionChanged="@SelectionChanged">
        <TabTextTemplate>
            <span class=@context.Icon aria-hidden="true">
                @if (context.PadgeCount != 0)
                {
                    <span class="@context.GetPadgeClass()">
                        @context.PadgeCount
                        <span class="visually-hidden">@context.Padge</span>
                    </span>
                }
            </span>
            @context.Text
        </TabTextTemplate>
        <ChildContent>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.Agent, GXRoles.AgentManager)>
                <TabPage Path="Agent" Text=@Properties.Resources.Agents Icon="oi oi-wifi">
                    <Agents Id="@Id"></Agents>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.AgentGroup, GXRoles.AgentGroupManager)>
                <TabPage Path="AgentGroup" Text=@Properties.Resources.AgentGroups>
                    <AgentGroups Id="@Id"></AgentGroups>
                </TabPage>
            </AuthorizeView>
            <AuthorizeView Roles=@GXRoles.ToString(GXRoles.Admin, GXRoles.AgentLog, GXRoles.AgentLogManager)>
                <TabPage Path="AgentLog"
                         Text=@Properties.Resources.AgentLogs
                         Padge="New agent logs"
                         PadgeCount="@_log.GetCount()"
                         PadgeType="@_log.GetTraceLevel()"
                         Icon="oi oi-warning">
                    <AgentLogs Id="@Id"></AgentLogs>
                </TabPage>
            </AuthorizeView>
        </ChildContent>
    </TabControl>
}
<br />
@code {

    /// <summary>
    /// User action.
    /// </summary>
    [Parameter]
    public string? Action { get; set; }
    /// <summary>
    /// Selected item.
    /// </summary>
    [Parameter]
    public Guid? Id { get; set; }

    private string? Path { get; set; }

    /// <summary>
    /// Stamp.
    /// </summary>
    private GXUserStamp _log = new GXUserStamp();

    /// <summary>
    /// User has selected a new tab.
    /// </summary>
    /// <param name="page">Tab page.</param>
    void SelectionChanged(TabPage page)
    {
        Path = page.Path;
    }

    protected override void OnInitialized()
    {
        //Get selected tab from the url.
        Path = NavigationManager.ToBaseRelativePath(NavigationManager.Uri);
        _log = Notifier.GetUserStamps(TargetType.AgentLog);
        Notifier.On<IEnumerable<GXUserStamp>>(this, nameof(IGXHubEvents.StampUpdate), (stamps) =>
        {
            bool updated = false;
            foreach (var it in stamps)
            {
                if (it.TargetType == TargetType.AgentLog)
                {
                    updated = true;
                }
            }
            if (updated)
            {
                StateHasChanged();
                StateHasChanged();
            }
        });
    }

    public void Dispose()
    {
        Notifier.RemoveListener(this);
    }
}

